package com.example.juc.demo;


import java.util.concurrent.locks.LockSupport;

/**
 * Synchronized
 *   wait   notify
 * Lock
 *   await  signal
 * LockSupport
 *   park   unpark
 *
 */
public class LockSupportDemo {

    public static void main(String[] args) {

       /**
        * 1.LockSupport使用方便不需要像Synchronized和Lock一样加解锁
        * 2.Synchronized和Lock唤醒线程如果在阻塞线程前运行，唤醒会失效
        * 而LockSupport更类似于门票，对时间没有要求，只对次数有要求
        *
        */
        Thread t1 = new Thread(() -> {
            //park有计数，如果park两次就要unpark两次不然还是会阻塞
            LockSupport.park();
            System.out.println(Thread.currentThread().getName()+"被唤醒了！");
        });
        t1.start();

        new Thread(()->{
            //唤醒t1
            LockSupport.unpark(t1);
        },"t1").start();

    }
}

